<template>
  <div class="user">
    <el-form :model="form" :rules="rules" ref="form" label-width="100px" class="demo-form">
        <el-form-item label="菜单名称：" prop="name">
            <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="菜单路径：" prop="href">
            <el-input v-model="form.href"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单：" prop="parentId">
            <el-select v-model="form.parentId">
            <el-option label="无上级菜单" :value="-1"></el-option>
            <el-option
              v-for="item in parentMenuList"
              :key="item.id"
              :label="item.name"
              :value="item.id"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="描述：" prop="description">
            <el-input v-model="form.description"></el-input>
        </el-form-item>
        <el-form-item label="前端图标：" prop="icon">
            <el-input v-model="form.icon"></el-input>
        </el-form-item>
        <el-form-item label="是否显示" prop="resource">
            <el-radio-group v-model="form.shown">
            <el-radio :label="true">是</el-radio>
            <el-radio :label="false">否</el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="排序：" prop="orderNum">
            <el-input v-model="form.orderNum"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm">立即创建</el-button>
            <el-button v-if="isEdit" @click="resetForm">重置</el-button>
        </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { getEditMenuInfo, saveOrUpdateMenu } from '@/services/menu'

export default Vue.extend({
  name: 'MenuForm',

  data () {
    return {
      isEdit: false,
      form: {
        id: '',
        parentId: -1,
        name: '',
        href: '',
        icon: '',
        orderNum: '',
        description: '',
        shown: true
      },
      rules: {},
      cacheForm: {
        id: '',
        parentId: -1,
        name: '',
        href: '',
        icon: '',
        orderNum: '',
        description: '',
        shown: true
      },
      parentMenuList: []
    }
  },

  mounted () {
    const id = this.$route.query.id
    this.isEdit = typeof id === 'number'
    this.getInfo(Number(id) || -1)
  },

  methods: {
    getInfo (id: number) {
      getEditMenuInfo(id).then(res => {
        if (res.data.code === '000000') {
          const { menuInfo, parentMenuList } = res.data.data
          this.parentMenuList = parentMenuList
          if (this.isEdit && menuInfo) {
            this.form = menuInfo
            this.cacheForm = menuInfo
          }
        }
      })
    },

    resetForm () {
      this.form = { ...this.cacheForm }
    },

    submitForm () {
      saveOrUpdateMenu(this.form).then(res => {
        if (res.data.code === '000000') {
          this.$message.success(this.isEdit ? '编辑成功' : '添加成功')
          this.$router.back()
        } else {
          this.$message.error(res.data.message)
        }
      })
    }
  }
})
</script>

<style lang="scss" scoped></style>
